package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.StatMapper;
import com.qf.pojo.DtsOrder;
import com.qf.service.DtsOrderService;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.service.impl
 * @Description:
 * @Date 2022/10/13 20:49
 */
@Service
public class DtsOrderServiceImpl implements DtsOrderService {

    @Autowired
    DtsOrderMapper orderMapper;
    @Autowired
    StatMapper statMapper;

    @Override
    public long getTotal() {
        LambdaQueryWrapper<DtsOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsOrder::getDeleted,false);
        Integer count = orderMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public Map<String, Object> selectOrderStat() {
        //        获取一个月前的今天
        Calendar calendar = Calendar.getInstance();

        calendar.add(Calendar.MONTH,-1);

        Date date = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String format = sdf.format(date);
        List<DayStatis> rows = statMapper.findOrderStatis(format);
        String[] columns = {"day", "orders", "customers", "amount", "pcr"};

        Map<String, Object> data = new HashMap<>();
        data.put("columns",columns);
        data.put("rows",rows);
        return data;
    }

    @Override
    public Map<String, Object> selectOrderByPage(Integer page, Integer limit, String sort, String order, Integer userId, Integer orderSn, Integer orderStatusArray) {
        IPage<DtsOrder> iPage = new Page<>(page,limit);
        QueryWrapper<DtsOrder> wrapper = new QueryWrapper<>();
        if ("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else {
            wrapper.orderByAsc(sort);
        }
        wrapper.eq("deleted",false);
        if (userId!=null){
            wrapper.like("user_id",userId);
        }
        if (orderSn!=null){
            wrapper.like("order-sn",orderSn);
        }
        if (orderStatusArray!=null){
            wrapper.eq("order_status",orderStatusArray);
        }
        orderMapper.selectPage(iPage,wrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());
        return map;
    }

    @Override
    public List<Map<String, Object>> selectShipChannel() {
        QueryWrapper<DtsOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("deleted",false);


        List<DtsOrder> orderList = orderMapper.selectList(wrapper);
        List<Map<String, Object>> collect = orderList.stream().map(order -> {
            Map<String, Object> map = new HashMap<>();
            map.put("value", order.getShipChannel());
            map.put("label", order.getMessage());
            return map;
        }).collect(Collectors.toList());

        return collect;
    }


}
